package com.homestead.client;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LeetCodeClient {
    public static void main(String[] args) {
        /*//==========1. 两数之和===========
        int[] nums = {2, 7, 11, 15};
        int[] result = twoSum(nums, 9);
        System.out.println(Arrays.toString(result));
        //===========7. 整数反转==========
        int fz = reverse(-1234);
        System.out.println(fz);

        boolean palindrome = isPalindrome(101);
        System.out.println(palindrome);*/

        int[] nums = {0,0,1,1,1,2,2,3,3,4};
        Solution solution = new Solution();
        int counts = solution.removeDuplicates(nums);
        System.out.println("去重后长度：counts=" + counts);
        Arrays.stream(nums).forEach(System.out::println);
    }

    /**
     * 1. 两数之和
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                return new int[]{map.get(nums[i]), i};
            }
            map.put(target - nums[i], i);
        }
        return null;
    }

    /**
     * 7. 整数反转
     * @param x
     * @return
     */
    public static int reverse(int x) {
        // x = 1234
        int n = 0;
        while(x != 0) {
            n = n * 10 + x%10;
            x = x / 10;
        }
        return (int)n==n? (int)n : 0;
    }

    public static boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        int temp = x;
        int n = 0;
        while (x > 0) {
            n = n *10 + x%10;
            x = x / 10;
        }
        return n == temp;
    }
}
